package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.impl.DesignerDaoImpl;
import com.ysd.entity.Designer;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminDesignerServlet extends BaseServlet {
	/**
	 * 查询设计师
	  localhost:8080/meizizi/adminDesigner?reqName=getDesigners 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getDesigners(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		DesignerDaoImpl dDao = new DesignerDaoImpl(); // 调用实现类创建对象
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<Designer> dList = dDao.getDesignerByPage(page, size); // 查到所有设计师的集合

		// 发送ajax请求,服务器基本都会返回json格式字符串

		response.setCharacterEncoding("UTF-8"); // 设置相应的编码格式
		PrintWriter out = response.getWriter(); // out对象

		Gson gson = new Gson();
		String res = gson.toJson(dList);

		out.print(res); // 响应数据

		out.flush();
		out.close();
	}
	
	/**
	 * 查询设计师
	  localhost:8080/meizizi/adminDesigner?reqName=getAllDesigners
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getAllDesigners(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		DesignerDaoImpl dDao = new DesignerDaoImpl(); // 调用实现类创建对象
		List<Designer> dList = dDao.getDesigner(); // 查到所有设计师的集合

		// 发送ajax请求,服务器基本都会返回json格式字符串

		response.setCharacterEncoding("UTF-8"); // 设置相应的编码格式
		PrintWriter out = response.getWriter(); // out对象

		Gson gson = new Gson();
		String res = gson.toJson(dList.size());

		out.print(res); // 响应数据

		out.flush();
		out.close();
	}
	

	/**
	 * localhost:8080/meizizi/adminDesigner?reqName=getDes
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getDes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int dId = Integer.parseInt(request.getParameter("designerid")); // 接收前台传来的参数
		DesignerDaoImpl dDao = new DesignerDaoImpl(); // 调用实现类创建对象
		int d = dDao.deleteDesignerById(dId); // 调用删除的方法

		response.setCharacterEncoding("UTF-8"); // 设置相应的编码格式
		PrintWriter out = response.getWriter(); // out对象

		if (d > 0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));// {"success":true,"message":"删除成功","remark":""}
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));// {"success":false,"message":"删除失败","remark":""}
		}

		out.flush();
		out.close();
	}

	/**
	 * localhost:8080/meizizi/adminDesigner?reqName=addDesigner
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addDesigner(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8"); // 设置相应的编码格式
		PrintWriter out = response.getWriter(); // out对象
		DesignerDaoImpl desDao = new DesignerDaoImpl();// 创建设计师实现类对象
		String designername = request.getParameter("designername");// 接受前台传来的参数
		String title = request.getParameter("title");
		String headportrait = request.getParameter("headportrait");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		if (designername != null && !designername.equals("")) {
			List<Designer> desList = desDao.getDesignerByName(designername);
			if (desList.size() == 0) {// ==0就是查到了 可以添加
				Designer de = new Designer(0, designername, title, headportrait, phone, email);// 创建设计师实体类对象

				int d = desDao.addDesigner(de);
				if (d > 0) {// 添加成功
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {// 没查到 不能添加
				out.print(Result.toClient(false, "用户名已存在", ""));
			}
		}
		out.flush();
		out.close();
	}

	/**
	 * localhost:8080/meizizi/adminDesigner?reqName=addUploadss
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addUploadss(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();

		// 1.创建SmarUpload对象
		SmartUpload smart = new SmartUpload();
		// 2.设置编码格式
		smart.setCharset("UTF-8");
		// 3.初始化
		smart.initialize(getServletConfig(), request, response);

		try {// 正常
				// 4.上传
			smart.upload();
			// 5.设置上传文件名
			String fileName = System.currentTimeMillis() + ".jpg";
			// 6.将文件保存到指定目录
			smart.getFiles().getFile(0).saveAs("/images/" + fileName);
			// 7.响应数据
			out.print(Result.toClient(true, "上传成功", fileName));
		} catch (SmartUploadException e) {// 异常
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败", "图片过大"));
		}
		out.flush();
		out.close();
	}
	/**
	 * localhost:8080/meizizi/adminDesigner?reqName=getDesignersByid
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getDesignersByid(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8"); // 设置相应的编码格式
		PrintWriter out = response.getWriter(); // out对象
		int id = Integer.parseInt(request.getParameter("designerid")); // 接收前台传来的参数

		DesignerDaoImpl dDao = new DesignerDaoImpl(); // 调用实现类创建对象
		Designer des=dDao.getDesignerByid(id);

		// 发送ajax请求,服务器基本都会返回json格式字符串

		
		Gson gson = new Gson();
		String res = gson.toJson(des);

		out.print(res); // 响应数据

		out.flush();
		out.close();
	}
	/**
	 * localhost:8080/meizizi/adminDesigner?reqName=updateDesignersByid
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateDesignersByid(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8"); // 设置相应的编码格式
		PrintWriter out = response.getWriter(); // out对象
		int designerid = Integer.parseInt(request.getParameter("designerid"));// 接收前台传来的参数
		String designername=request.getParameter("designername");
		String title=request.getParameter("title");
		String headportrait=request.getParameter("headportrait");
		String phone=request.getParameter("phone");
		String email=request.getParameter("email");
		DesignerDaoImpl dDao = new DesignerDaoImpl(); // 调用实现类创建对象
		List<Designer> desList=dDao.getDesignerByName(designername);
		if (desList.size()==0) {//没有查到同名的可以修改
			Designer des=new Designer(designerid, designername, title, headportrait, phone, email);
			int n=dDao.updateDesignerById(des);
			if (n>0) {//n>0修改成功
				out.print(Result.toClient(true, "修改成功", ""));
			} else {//失败
				out.print(Result.toClient(false, "修改失败", ""));
			}
		} else {//查到同名的不能修改
			out.print(Result.toClient(false, "重复的名字", ""));
		}
		
		

		
		

		out.flush();
		out.close();
	}

}
